package jisuanji;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;

import java.awt.Shape;
import java.util.ArrayList;

class ShapeCoverageTest {

    private static final double EPS = 1e-9;

    /* ---------- 各形状数值测试 ---------- */
    @Test
    void testCircle() {
        Circle c = new Circle(10);
        assertEquals(Math.PI * 100, c.getArea(), EPS);
        assertEquals(2 * Math.PI * 10, c.getPerimeter(), EPS);
        Shape s = c.getRealShape();
        assertNotNull(s);
    }

    @Test
    void testSquare() {
        Square sq = new Square(5);
        assertEquals(25, sq.getArea(), EPS);
        assertEquals(20, sq.getPerimeter(), EPS);
        assertNotNull(sq.getRealShape());
      
    }

    @Test
    void testRectangle() {
        Rectangle r = new Rectangle(4, 6);
        assertEquals(24, r.getArea(), EPS);
        assertEquals(20, r.getPerimeter(), EPS);
        assertNotNull(r.getRealShape());
    }

    @Test
    void testTriangle() {
        Triangle t = new Triangle(3, 4, 5);
        assertEquals(6, t.getArea(), EPS);
        assertEquals(12, t.getPerimeter(), EPS);
        assertNotNull(t.getRealShape());
    }

    @Test
    void testSector() {
        Sector sec = new Sector(10, 90);   // 1/4 圆
        assertEquals(Math.PI * 100 / 4, sec.getArea(), EPS);
        // 周长 = 弧长 + 2*r
        assertEquals(2 * Math.PI * 10 * 0.25 + 20, sec.getPerimeter(), EPS);
        assertNotNull(sec.getRealShape());
    }

    @Test
    void testTrapezoid() {
        Trapezoid tr = new Trapezoid(10, 20, 5);
        assertEquals(75, tr.getArea(), EPS);
        // 腰长 = sqrt(5^2 + 5^2) = 7.071...
        assertEquals(10 + 20 + 2 * Math.sqrt(50), tr.getPerimeter(), EPS);
        assertNotNull(tr.getRealShape());
    }

    /* ---------- GUI 逻辑测试 ---------- */
    private RealCanvas canvas;

    @BeforeEach
    void initCanvas() {
        canvas = new RealCanvas();
        // 模拟组件大小，否则 reScale 里 getWidth/getHeight 返回 0
        canvas.setSize(800, 600);
    }

    @Test
    void canvasAddAndRemove() {
        assertEquals(0, canvas.list.size());
        canvas.addShape(new Circle(10));
        assertEquals(1, canvas.list.size());
        canvas.removeLast();
        assertEquals(0, canvas.list.size());
    }

    @Test
    void canvasClear() {
        canvas.addShape(new Square(5));
        canvas.addShape(new Rectangle(3, 4));
        assertFalse(canvas.list.isEmpty());
        canvas.clear();
        assertTrue(canvas.list.isEmpty());
    }

    /* ---------- 总汇总测试 ---------- */
    @Test
    void integration() {
        RealCanvas c = new RealCanvas();
        c.setSize(800, 600);
        c.addShape(new Circle(10));          // 314.16
        c.addShape(new Square(10));          // 100
        c.addShape(new Rectangle(5, 10));    // 50
        double expectArea = 314.16 + 100 + 50;
        double realArea = c.list.stream().mapToDouble(MyShape::getArea).sum();
        assertEquals(expectArea, realArea, 1);
    }
}